package com.example.demo.javaTest;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionMap {

    @Test
    public void CollectionMap() {
        Collection<String> a = new ArrayList<>();
        a.add("1");
        a.add("2");
        a.add("3");
        System.out.println(a);
        a.clear();
        System.out.println(a);
        System.out.println(a.isEmpty());
        a.add("4");
        System.out.println(a.size());
        System.out.println(a.contains("1"));
        a.remove("4");
        a.add("5");
        a.add("6");
        Object[] arr = a.toArray();
        System.out.println(Arrays.toString(arr));
        String[] arr2 = a.toArray(new String[a.size()]);
        System.out.println(Arrays.toString(arr2));

        Collection<String> b = new ArrayList<>();
        Collection<String> c = new ArrayList<>();
        b.add("1");
        b.add("2");
        c.add("1");
        c.add("2");
        b.addAll(c);
        Collections.addAll(c, "3", "4");
        Collections.shuffle((List<?>) b);
        System.out.println(b);
        System.out.println(c);

        for (String s : b) {
            System.out.println(s);
        }
        Iterator<String> it = b.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        c.forEach(s -> System.out.println(s));
        c.forEach(System.out::println);

        List<String> list = new ArrayList<>();//多态，可随时替换为LinkedList
        list.add("!");
        list.add("2");
        list.add("3");
        list.add(1, "4");
        System.out.println(list);
        list.remove(2);
        System.out.println(list);
        System.out.println(list.get(2));
        list.set(1, "a");
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        for (String s : list) {
            System.out.println(s);
        }
        Iterator<String> it2 = list.iterator();
        while (it2.hasNext()) {
            System.out.println(it2.next());
        }
        list.forEach(System.out::println);

        LinkedList<String> queue = new LinkedList<>();
        queue.addLast("1");
        queue.addLast("2");
        queue.addLast("3");
        queue.addLast("4");
        System.out.println(queue);
        queue.removeFirst();
        System.out.println(queue);

        LinkedList<String> stack = new LinkedList<>();
        stack.addFirst("1");
        stack.addFirst("2");
        stack.addFirst("3");
        stack.addFirst("4");
        System.out.println(stack);
        queue.removeFirst();
        System.out.println(stack);
        stack.push("1");
        stack.pop();
        System.out.println(stack);

        Set<Integer> set = new HashSet<>();
        set.add(4);
        set.add(2);
        set.add(3);
        set.add(2);
        set.add(1);
        System.out.println(set);//如果是对象，地址不同则不能去重，除非自行重写equals和hashCode，可通过鼠标右键generator，由idea自动重写

        Set<Integer> set2 = new LinkedHashSet<>();
        set2.add(4);
        set2.add(2);
        set2.add(3);
        set2.add(2);
        set2.add(1);
        System.out.println(set2);

        Set<Integer> set3 = new TreeSet<>();
        set3.add(4);
        set3.add(2);
        set3.add(3);
        set3.add(2);
        set3.add(1);
        System.out.println(set3);

        Map<String, Integer> map = new HashMap<>();
        map.put("1", 1);
        map.put("2", 2);
        map.put("2", 22);
        map.put("3", 3);
        map.put(null, null);
        System.out.println(map);
        System.out.println(map.size());
        map.clear();
        System.out.println(map.isEmpty());
        for (String key : map.keySet()) {
            System.out.println(key);
        }
        for (Map.Entry<String, Integer> pairs : map.entrySet()) {
            System.out.println(pairs.getKey());
            System.out.println(pairs.getValue());
        }
        map.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });

        Map<String, Integer> map2 = new LinkedHashMap<>();
        map2.put("1", 1);
        map2.put("2", 2);
        map2.put("2", 22);
        map2.put("3", 3);
        map2.put(null, null);
        System.out.println(map2);
        System.out.println(map2.get("1"));
        map2.remove("2");
        System.out.println(map2.containsKey("2"));
        System.out.println(map2.containsValue(1));
        System.out.println(map2.keySet());
        System.out.println(map2.values());
        map2.putAll(new LinkedHashMap<>());

        Map<String, Integer> map3 = new TreeMap<>();
        map3.put("12", 1);
        map3.put("21", 2);
        map3.put("21", 22);
        map3.put("03", 3);
        System.out.println(map3);
    }

    @Test
    public void testStream() {
        List<String> names = new ArrayList<>();
        Collections.addAll(names, "abc", "def", "ghi", "jkl", "mno", "pqr", "tuv", "wxyz", "acd", "abd", "aa", "ab");
        List<String> list = names.stream().filter(s -> s.startsWith("a")).filter(a -> a.length() == 3).collect(Collectors.toList());
        System.out.println(list);

        Set<String> set = new HashSet<>();
        Collections.addAll(set, "abc", "def", "ghi", "jkl", "mno", "pqr", "tuv", "wxyz", "acd", "abd", "aa", "ab");
        Stream<String> stream = set.stream();
        stream.filter(s -> s.contains("b")).forEach(System.out::println);

        Map<String, Double> map = new HashMap<>();
        map.put("a", 1.0);
        map.put("b", 1.1);
        map.put("c", 2.0);
        map.put("d", 1.2);
        Set<String> key = map.keySet();
        Stream<String> ks = key.stream();
        Collection<Double> values = map.values();
        Stream<Double> vs = values.stream();
        Set<Map.Entry<String, Double>> entries = map.entrySet();
        Stream<Map.Entry<String, Double>> kvs = entries.stream();
        kvs.filter(e -> e.getKey().contains("a")).forEach(e -> System.out.println(e.getKey() + "-" + e.getValue()));
        Double v = vs.max(Double::compare).get();
        System.out.println(v);

        String[] names2 = {"abc", "def", "ghi", "jkl", "mno", "pqr", "tuv", "wxyz", "acd", "abd", "aa", "ab"};
        Stream<String> namesStream = Arrays.stream(names2);
        Stream<String> namesStream2 = Stream.of(names2);

        //常用操作方法:filter/sorted/limit/skip/distinct/map/concat
        //终结返回方法:forEach/count/max/min
        //收集流:collect(Collectors.toXXX()),XXX可为List/Set,Collectors.toMap(a->a.YYY返回键的方法,a->a.ZZZ返回值的方法),转数组直接toArray(len->new 对象[len])
    }

    @Test
    public void testHashCrack() {
        String a = new String("abc");
        String b = new String("acD");
        System.out.println(a.hashCode());//96354
        System.out.println(b.hashCode());//96354
    }

}
